package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * JDK5之后推出了另一个特性:增强型for循环
 * 也称为新循环，【用于遍历集合或数组】
 *
 * 语法:
 * for(元素类型 e : 数组或集合){
 *     循环体
 * }
 */
public class NewForDemo {
    public static void main(String[] args) {
        String[] array = {"one","two","three","four","five"};
        //普通for循环
        for (int i = 0; i < array.length; i++) {
            String e = array[i];
            System.out.println(e);
        }
        //增强型for循环遍历数组
        for(String e : array){
            System.out.println(e);
        }

        Collection<String> c = new ArrayList<>();
        c.add("1");
        c.add("2");
        c.add("3");
        c.add("4");
        c.add("5");
        //迭代器遍历集合
        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            String e = it.next();
            System.out.println(e);
        }
        //增强型for循环遍历集合
        for(String e : c){
            System.out.println(e);
        }
    }
}
